package com.bootdo.common.page.component;

import org.springframework.context.ApplicationContext;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.AttributeValueQuotes;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.spring5.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: bootdo
 * @description:
 * @author: wfy
 * @create: 2018-09-14 17:45
 */
public class FileElementTagProcessor extends AbstractElementTagProcessor {
    private static final String TAG_NAME  = "file";//标签名
    private static final int PRECEDENCE = 10000;//优先级


    public FileElementTagProcessor(String dialectPrefix) {
        super(
                TemplateMode.HTML, // 此处理器将仅应用于HTML模式
                dialectPrefix,     // 要应用于名称的匹配前缀
                TAG_NAME,          // 标签名称：匹配此名称的特定标签
                true,              // 将标签前缀应用于标签名称
                null,              // 无属性名称：将通过标签名称匹配
                false,             // 没有要应用于属性名称的前缀
                PRECEDENCE);       // 优先(内部方言自己的优先)
    }

    /**
     * context 页面上下文
     * tag  标签
     *
     */
    @Override
    protected void doProcess(
            final ITemplateContext context,
            final IProcessableElementTag tag,
            final IElementTagStructureHandler structureHandler) {

        /**
         * 获取应用程序上下文。
         */
        ApplicationContext appCtx = SpringContextUtils.getApplicationContext(context);
        final IModelFactory modelFactory = context.getModelFactory();
        final IModel model = modelFactory.createModel();
//        <!--用来存放文件信息-->
//        <div id="thelist" class="uploader-list"></div>
//        <div class="btns">
//            <div id="picker">选择文件</div>
//            <button id="ctlBtn" class="btn btn-default">开始上传</button>
//        </div>
        Map<String, String> fileInfoMap=new HashMap<>();
        fileInfoMap.put("id","thelist");
        fileInfoMap.put("class","uploader-list");
        model.add(modelFactory.createOpenElementTag("div",fileInfoMap, AttributeValueQuotes.NONE,false));
        model.add(modelFactory.createCloseElementTag("div"));

        model.add(modelFactory.createOpenElementTag("div","class","btns"));
        model.add(modelFactory.createOpenElementTag("div","id","picker"));
        model.add(modelFactory.createText("选择文件"));
        model.add(modelFactory.createCloseElementTag("div"));
        Map<String, String> buttonInfoMap=new HashMap<>();
        buttonInfoMap.put("id","ctlBtn");
        buttonInfoMap.put("class","btn btn-default");
        model.add(modelFactory.createOpenElementTag("button",buttonInfoMap, AttributeValueQuotes.DOUBLE,false));
        model.add(modelFactory.createText("开始上传"));
        model.add(modelFactory.createCloseElementTag("button"));
        model.add(modelFactory.createCloseElementTag("div"));

       /*
        * 指示引擎用指定的模型替换整个元素。
        */
        structureHandler.replaceWith(model, false);
    }

}
